//
//  KRSearchResultController.m
//  cookBook
//
//  Created by kuroken on 16/1/26.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRSearchResultController.h"
#import <MJExtension.h>
#import "KRSearchResult.h"
#import "KRConst.h"

static NSString * const KRSearchResultID = @"searchResult";

@interface KRSearchResultController ()
/** 当前请求 */
@property (nonatomic, weak) NSURLSessionDataTask *currentDataTask;

@end

@implementation KRSearchResultController


- (instancetype)init
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:KRSearchResultID];
    
    [self.view setBackgroundColor:KRColorRGB(240)];
    
}

- (void)setResult:(KRSearchResult *)result
{
    _result = result;
    
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    NSLog(@"%s", __func__);
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.result.count integerValue];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KRSearchResultID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KRSearchResultID];
    }
    [cell.imageView setImage:[UIImage imageNamed:@"ico_cell_search"]];
    [cell.textLabel setText:self.result.list[indexPath.row]];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(searchResultControllerDidSelectRowAtIndexPath:)]) {
        [self.delegate searchResultControllerDidSelectRowAtIndexPath:indexPath];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
}

/**
 *  每组头部标题
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

/**
 *  每组头部的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

/**
 *  每组尾部的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

@end